/**
 * 
 */
package com.hector.meinewerk.common;

import com.hector.meinewerk.client.R;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.TypedValue;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class MeineWerkApplication extends Application {
	
	private static MeineWerkApplication mInstance;
	
	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
	}
	
	public static int getMinRow(){
		BitmapDrawable bd=(BitmapDrawable) mInstance.getResources().getDrawable(R.drawable.bshelf);
		final int height=bd.getBitmap().getHeight();
		
		final int heighScreen;
		if(getHeightScreen() > getWidthScreen()){
			heighScreen = getHeightScreen();
		} else{
			heighScreen = getWidthScreen();
		}
		
		return (heighScreen/height + 1);
	}
	
	public static int getHeightScreen() {
		return mInstance.getResources().getDisplayMetrics().heightPixels;
	}

	public static int getWidthScreen() {
		return mInstance.getResources().getDisplayMetrics().widthPixels;
	}
	
	// dip to px
		public static int convertDensityToPixel(Context context, int dip) {
			return (int) (dip * context.getResources().getDisplayMetrics().density + 0.5);
		}

		// px to dip
		public static int convertPixelToDensity(Context context, int pixel) {
			return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
					pixel, context.getResources().getDisplayMetrics());
		}

		public static void hideKeyboard(Activity activity) {
			try {
				InputMethodManager inputManager = (InputMethodManager) activity
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			} catch (Exception e) {
				return;
			}
		}

		public static void hideKeyboard(Context context, EditText e) {
			InputMethodManager imm = (InputMethodManager) context
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(e.getWindowToken(), 0);
		}

		public static void showKeyboard(Activity activity) {
			try {
				InputMethodManager inputManager = (InputMethodManager) activity
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputManager.showSoftInputFromInputMethod(activity
						.getCurrentFocus().getWindowToken(),
						InputMethodManager.SHOW_IMPLICIT);
				// inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
				// .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			} catch (Exception e) {
				return;
			}
		}

		public static void showKeyboardNew(Context context) {
			InputMethodManager in = (InputMethodManager) context
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			// in.toggleSoftInputFromWindow(getWindowToken(),
			// InputMethodManager.SHOW_FORCED,
			// InputMethodManager.HIDE_IMPLICIT_ONLY);
			in.toggleSoftInput(InputMethodManager.SHOW_FORCED,
					InputMethodManager.HIDE_IMPLICIT_ONLY);
		}
}
